package com.dahua.messagesyncsh.rabbitmq.messageListener;

import com.alibaba.fastjson2.JSON;
import com.dahua.messagesyncsh.pojo.T485;
import com.dahua.messagesyncsh.rabbitmq.pojo.EqStatusMessage;
import com.dahua.messagesyncsh.service.ProcessorService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.BatchMessageListener;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
public class MitsubishiMessageListener implements BatchMessageListener {

    @Resource
    private ProcessorService processorService;

    @Override
    public void onMessageBatch(List<Message> list) {
        List<T485> t485s = new ArrayList<>();
        for (Message message : list) {
            try {
                EqStatusMessage eqStatusMessage = JSON.parseObject(message.getBody(), EqStatusMessage.class);
                T485 t485 = processorService.convert2T485(eqStatusMessage);
                if (t485 != null) {
                    t485s.add(t485);
                }
            } catch (Exception e) {
                log.error("【Mitsubishi】: 数据处理解析错误{} \n {}", message.getBody(), e.toString());
            }
        }
        processorService.handleT485(t485s);
    }
}
